package com.tz.dream.architect.mvp.framework;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.tz.dream.architect.mvp.framework.support.ActivityMvpDelegate;
import com.tz.dream.architect.mvp.framework.support.ActivityMvpDelegateImpl;
import com.tz.dream.architect.mvp.framework.support.MvpDelegateCallback;

/**
 * 作者: Dream on 2018/3/16 21:12
 * QQ:510278658
 * E-mail:510278658@qq.com
 */

//中介者模式->具体同事A（抽象）->绑定V层和解绑V层
//代理模式->第一重代理->代理对象->生命周期(持有目标对象引用)->MvpActivity
//代理模式->第二重代理->目标对象->绑定和解绑->MvpActivity(实现目标接口)
public abstract class MvpActivity<V extends MvpView, P extends MvpPresenter<V>>
        extends Activity implements MvpView, MvpDelegateCallback<V, P> {

    //目标对象
    protected ActivityMvpDelegate<V, P> mvpDelegate;

    protected ActivityMvpDelegate<V, P> getMvpDelegate() {
        if (this.mvpDelegate == null){
            this.mvpDelegate = new ActivityMvpDelegateImpl<V, P>(this, this, true);
        }
        return mvpDelegate;
    }


    /***********第二重代理****start*******/
    private P presenter;

    @Override
    public P createPresenter() {
        return presenter;
    }

    @Override
    public P getPresenter() {
        return presenter;
    }

    @Override
    public void setPresenter(P presenter) {
        this.presenter = presenter;
    }

    @Override
    public V getMvpView() {
        return (V)this;
    }
    /************end**********/

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getMvpDelegate().onCreate(savedInstanceState);
    }

    @Override
    protected void onStart() {
        super.onStart();
        getMvpDelegate().onStart();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        getMvpDelegate().onRestart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getMvpDelegate().onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        getMvpDelegate().onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        getMvpDelegate().onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        getMvpDelegate().onDestroy();
    }

}
